<?php

class DataWidget extends CWidget {

	/**
	 * model名称
	 * 设置此项，则为$model::model()->findAll($this->criteria)
	 * @var string|object
	 */
	public $model;
	/**
	 * 查询条件
	 * 设置model属性后生效
	 * @var mixed
	 */
	public $criteria = NULL;
	/**
	 * 页面
	 * Enter description here ...
	 * @var string
	 */
	public $view;
	/**
	 * js
	 * Enter description here ...
	 * @var array
	 */
	public $scripts = array();
	/**
	 * 数据
	 * Enter description here ...
	 * @var array
	 */
	public $data = array();
	
	public $attributes = array();
	
	public function run(){
		$this->registerAssets();
		
		$model = $this->model;
		if(!is_object($model) && !empty($model) && !is_array($model)) {
			$model = new $model();
			$model->attributes = $this->attributes;
			$model = $model->findAll($this->criteria);
		}
		$this->render($this->view,array_merge($this->data,array(
			'model' => $model
		)));
	}
	
	protected function registerAssets() {
		if(!$this->scripts) return;
		$cs = Yii::app()->clientScript;
		$am = Yii::app()->assetManager;
		foreach($this->scripts as $one) {
			if(is_string($one)) {
				if(strncasecmp('publish:', $one, 8)) {
					$path = substr($one, 8);
					$url = $am->publish($path, false, -1, true);
				} else {
					$url = $one;
				}
				$cs->registerScriptFile($url);
			} else {
				if(!$one[0]) $one[0] = mktime().mt_rand(1000, 9999); 
				call_user_func_array(array($cs, 'registerScript'), $one);
			}
		}
	}
	
}

?>